<?php

namespace App\Providers;

use App\Models\Attr;
use App\Models\Cart;
use App\Observers\AttrObserver;
use Houdunwang\Aliyun\Aliyun;
use Illuminate\Support\ServiceProvider;
use Illuminate\View\View;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        \Schema::defaultStringLength(191);
        // 调用获取权限模块的方法
        $this->getModules();
        // 调用方法匹配阿里云的接口
        $this->loadConfig();
    }

    public function getModules(){

        view()->composer('*',function (View $view){
            // 获取所有模块数据
            $modules=glob(app_path('Http/Controllers/*'));
//            dd($modules);
            // 建立空数组,用来存储所有有权限的模块数据的配置项
            $systems=[];
            foreach($modules as $v){
                if(is_dir($v.'/System')){
                    $systems[]=include $v.'/System/Config.php';
                }
            }
//            dd($systems);
            $view->with(compact('systems'));
        });


        // 获取当前用户购物车数据
        view()->composer('*',function(View $view){
            // 判断用户是否登录,登录则获取购物车数据,否则返回空数组
            if(auth()->check()){
                $carts=Cart::where('user_id',auth()->id())->get();
            }else{
                $carts=[];
            }
            $view->with(compact('carts'));

        });

    }

    public function loadConfig(){
        Aliyun::config([
            // 地区id
            'regionId'=>'cn-beijing',
            // 子账号id
            'accessId'=>'LTAITYLqrbleF3i0',
            // 子账号密码
            'accessKey'=>'CfirUl5c1rVKgkIyzbL5nEKZsshcwK'
        ]);
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {

        if ($this->app->environment() !== 'production') {
            $this->app->register(\Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class);
        }
        // ...
    }
}
